<?php

namespace Biz\service;

use Biz\model\brandIndex\BrandBanner;
use Doraemon\tools\ImgUrl;
use Biz\model\brandIndex\BrandQuestionCategory;
use Biz\model\brandIndex\BrandQuestions;
use Biz\model\brandIndex\BrandMsg;

class BrandIndex
{
    const DEFAULT_PAGESIZE = 20;

    //品牌服务平台首页banner
    public static function getBanners()
    {
        $banner_list = [];
        $banners = BrandBanner::getBannersOnline();
        if ($banners && is_array($banners)) {
            foreach ($banners as $index => $banner_info) {
                $banner_list[] = [
                    "id" => $banner_info['id'],
                    "index" => $index + 1,
                    "banner" => ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $banner_info['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI),
                    "link" => $banner_info['link'] ?: ''
                ];
            }
        }
        return array_values($banner_list);
    }

    //品牌服务平台首页消息通知
    public static function getMsgs($brand_id, $start, $page_size)
    {
        $msg_list = [];
        $msgs = BrandMsg::getMsgsByBrandId($start, $page_size, $brand_id);
        if ($msgs && is_array($msgs)) {
            foreach ($msgs as $msg_info) {
                $msg_list[] = [
                    "id" => (int)$msg_info['id'],
                    "brand_id" => (int)$msg_info['brand_id'],
                    "msg_type" => (int)$msg_info['msg_type'],
                    "msg_source" => (int)$msg_info['msg_source'],
                    "msg_info" => $msg_info['msg_info'],
                    "addtime" => date('Y.m.d', strtotime($msg_info['addtime'])),
                    "obj_type" => (int)$msg_info['obj_type'],
                    "obj_id" => $msg_info['obj_id']
                ];
            }
        }
        return array_values($msg_list);
    }

    //品牌服务平台问题分类
    public static function getQuestionCategorys()
    {
        $category_list = [];
        $categorys = BrandQuestionCategory::getCategorys();
        if ($categorys && is_array($categorys)) {
            foreach ($categorys as $category_info) {
                $category_list[] = [
                    "id" => $category_info['id'],
                    "category_name" => $category_info['category_name']
                ];
            }
        }
        return array_values($category_list);
    }

    //品牌服务平台问答列表
    public static function getQuestions($start, $page_size, $category_type = 1)
    {
        $question_list = [];
        $questions = BrandQuestions::getQuestionsByType($start, $page_size, $category_type);
        if ($questions && is_array($questions)) {
            foreach ($questions as $question_info) {
                $question_list[] = [
                    "id" => $question_info['id'],
                    "question_content" => $question_info['question_content'],
                    "answer_content" => $question_info['answer_content'],
                    "pic_url" => ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $question_info['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI)
                ];
            }
        }
        return array_values($question_list);
    }

    //写入通知消息
    public static function writeMsg($brand_id = 0, $msg_type = BrandMsg::MSG_TYPE_PASS, $msg_source = 0, $msg_info = '', $obj_type = -1, $obj_id = '')
    {
        return BrandMsg::addData([
            "brand_id" => $brand_id,
            "msg_type" => $msg_type,
            "msg_source" => $msg_source,
            "addtime" => date('Y-m-d H:i:s', time()),
            "msg_info" => $msg_info,
            "obj_type" => $obj_type,
            "obj_id" => $obj_id
        ]);
    }

    //搜索问答
    public static function searchQuestions($keyword = '')
    {
        $list = [];
        if (!$keyword) {
            return [];
        }
        //搜索回答
        $answer_list = BrandQuestions::searchByKeyword($keyword);
        $answer_list = self::formatData($answer_list, 1);
        //搜索标题
        $question_list = BrandQuestions::searchByKeyword($keyword, false);
        $question_list = self::formatData($question_list, 2);
        //搜索分类
        $category_list = BrandQuestionCategory::searchCategorys($keyword);
        $category_list = self::formatData($category_list, 3);
        $list = array_filter(array_merge($category_list, $question_list, $answer_list));
        return array_values($list);
    }

    /**
     * @param $list
     * @param $type 1：回答 2：问题 3：分类
     */
    public static function formatData($list, $type)
    {
        $result = [];
        switch ($type) {
            case 1:
                $title = 'answer_content';
                break;
            case 2:
                $title = 'question_content';
                break;
            case 3:
                $title = 'category_name';
                break;
            default:
                $title = '';
                break;

        }

        $categorys_arr = [];
        $categorys = BrandQuestionCategory::getCategorys();
        if($categorys && is_array($categorys)) {
            foreach($categorys as $category) {
                $categorys_arr[$category['id']] = $category;
            }
        }

        if ($list && is_array($list)) {
            foreach ($list as $info) {
                $result[] = [
                    "type" => (int) $type,
                    "id" => (int) $info["id"],
                    "title" => (string)$info[$title] ?: '',
                    "category_id" => (int) $info['category_type'] ? : ((int) $info['id'] ? : 0),
                    "category_name" => $type == 3 ? (string) $info['category_name'] : (string) $categorys_arr[$info['category_type']]['category_name']
                ];
            }
        }
        return $result;
    }


}